package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.PathUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.FileAdapter;
import flc.ast.databinding.ActivityMoveBinding;
import flc.ast.dialog.AddFolderDialog;
import flc.ast.utils.Content;
import stark.common.basic.media.MediaInfo;
import stark.common.basic.utils.FileUtil;

public class MoveActivity extends BaseAc<ActivityMoveBinding> {

    public static String videoPath;
    private String mCurrentRootPath;
    private FileAdapter fileAdapter;
    private List<String> mList = new ArrayList<>();

    @Override
    protected int onCreate() {
        return R.layout.activity_move;
    }

    @Override
    public void onBackPressed() {
        if (mCurrentRootPath.equals(PathUtils.getFilesPathExternalFirst() + Content.myFolder)) {
            super.onBackPressed();
        } else {
            mCurrentRootPath = FileUtils.getDirName(mCurrentRootPath);
            mCurrentRootPath = mCurrentRootPath.substring(0, mCurrentRootPath.length() - 1);
            getMoveData(mCurrentRootPath);
        }
    }

    @Override
    protected void initView() {
        mDataBinding.ivMoveBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivMove.setOnClickListener(this);
        mDataBinding.tvAddFolder.setOnClickListener(this);

        mDataBinding.rvFileList.setLayoutManager(new LinearLayoutManager(mContext));
        fileAdapter = new FileAdapter();
        mDataBinding.rvFileList.setAdapter(fileAdapter);
        fileAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvAddFolder://添加文件夹
                showAddFolderDialog();
                break;
            case R.id.ivMove://移动
                String filePath = FileUtil.generateFilePath(Content.myFolder + "/" + FileUtils.getFileNameNoExtension(mCurrentRootPath), "." + FileUtils.getFileExtension(videoPath));
                FileUtils.move(videoPath, filePath);
                getMoveData(mCurrentRootPath);
                finish();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        String path = fileAdapter.getItem(position);
        if (FileUtils.isDir(path)) {
            mCurrentRootPath = path;
            getMoveData(mCurrentRootPath);
        } else {
            PlayActivity.videoPath = path;
            startActivity(PlayActivity.class);
        }
    }

    @Override
    protected void initData() {
        mCurrentRootPath = PathUtils.getFilesPathExternalFirst() + Content.myFolder;
    }

    @Override
    protected void onResume() {
        super.onResume();
        getMoveData(mCurrentRootPath);
    }

    private void getMoveData(String rootPath) {
        if (rootPath.equals(PathUtils.getFilesPathExternalFirst() + Content.myFolder)) {
            mDataBinding.tvMoveTitle.setText(getString(R.string.root_path));
            mDataBinding.tvAddFolder.setVisibility(View.VISIBLE);
        } else {
            mDataBinding.tvMoveTitle.setText(FileUtils.getFileName(rootPath));
            mDataBinding.tvAddFolder.setVisibility(View.GONE);
        }
        List<File> files = FileUtils.listFilesInDir(rootPath);
        if (files.size() == 0) {
            mDataBinding.tvFileNoData.setVisibility(View.VISIBLE);
            mDataBinding.rvFileList.setVisibility(View.GONE);
        } else {
            mDataBinding.tvFileNoData.setVisibility(View.GONE);
            mDataBinding.rvFileList.setVisibility(View.VISIBLE);
            mList.clear();
            for (File file : files) {
                mList.add(file.getPath());
            }
            mDataBinding.rvFileList.scrollToPosition(0);
            fileAdapter.setList(mList);
        }
    }

    private void showAddFolderDialog() {
        AddFolderDialog dialog = new AddFolderDialog(mContext);
        dialog.setListener(new AddFolderDialog.Ilistener() {
            @Override
            public void clickAddFolder(String name) {
                String path = PathUtils.getFilesPathExternalFirst() + Content.myFolder + "/" + name;
                if (FileUtils.isFileExists(path)) {
                    ToastUtils.showShort(getString(R.string.folder_is_exist));
                } else {
                    FileUtils.createOrExistsDir(path);
                    ToastUtils.showShort(getString(R.string.add_folder_suc));
                }
                getMoveData(mCurrentRootPath);
            }
        });
        dialog.show();
    }


}